<?php

//处理收到的调货申请记录模块，Input = trans_id、操作，Output = 操作结果
header("content-type:application/json;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

$trans_id = $_POST['trans_id'];
$trans_op = $_POST['trans_op'];
if ($trans_op == "yes") {     //同意申请
    //获取调货申请信息
    $trans_detail = mysqli_fetch_assoc(mysqli_query($conn, "SELECT good_id,num,ans_shop FROM transfer_record WHERE id = '$trans_id'"));
    $good_id = $trans_detail['good_id'];
    $shop_ans = $trans_detail['ans_shop'];
    //获取本店内该商品信息
    $cloth_detail = mysqli_fetch_assoc(mysqli_query($conn, "SELECT num FROM clothes WHERE good_id = '$good_id' AND shop_id = '$shop_ans'"));
    $cloth_num = (int)($cloth_detail['num']);
    $trans_num = (int)($trans_detail['num']);
    //若数量够，则减少库存
    if ($cloth_num >= $trans_num) {
        mysqli_query($conn, "UPDATE clothes SET num = num-$trans_num WHERE good_id = '$good_id' AND shop_id = '$shop_ans'");
        mysqli_query($conn, "UPDATE transfer_record SET status = 'yes' WHERE id = '$trans_id'");
        echo json_encode(array('yes_status'=>'调货成功'));
    } else {      //否则不能同意调货
        mysqli_query($conn, "UPDATE transfer_record SET status = 'no' WHERE id = '$trans_id'");
        echo json_encode(array('yes_status'=>'调货失败，库存不足，已自动拒绝调货申请'));
    }
} else {      //拒绝申请
    //状态update为no
    mysqli_query($conn, "UPDATE transfer_record SET status = 'no' WHERE id = '$trans_id'");
    echo json_encode(array('yes_status'=>'已拒绝对方的申请'));
}
mysqli_close($conn);